package com.mresources.resources.handler;

import com.mresources.common.constants.FileHandlerEnum;
import com.mresources.resources.handler.impl.AliOssFileHandler;
import com.mresources.resources.handler.impl.DefaultFileHandler;
import com.mresources.resources.handler.impl.TxOssFileHandler;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class FileHandlerFactory {
    private final Map<Enum<FileHandlerEnum>, FileHandler> handlerMap = new HashMap<>();

    public FileHandler getInstance(FileHandlerEnum type) {
        if (type.equals(FileHandlerEnum.ALI)) {
            return handlerMap.computeIfAbsent(type, x -> new AliOssFileHandler(x.name()));
        } else if (type.equals(FileHandlerEnum.TX)) {
            return handlerMap.computeIfAbsent(type, x -> new TxOssFileHandler(x.name()));
        }
        return handlerMap.computeIfAbsent(type, x -> new DefaultFileHandler(x.name()));
    }

    public FileHandler getInstance() {
        return getInstance(FileHandlerEnum.DEF);
    }

}
